package com.ijunfu.lambda._07_group;

import java.util.*;
import java.util.stream.Collectors;

/**
 *
 * @Title  : 
 * @Author : ijunfu <ijunfu@163.com>
 * @Date   : 2023/11/18 14:14
 * @Version: 1.0
 * @Motto  : 世界很大 世界很小
 *
 */
public class Main {

    public static void main(String[] args) {

        List<String> fruits = Arrays.asList("banana", "apple", "pear", "grapefruit", "orange");

        Map<Integer, List<String>> map = new HashMap<>();
        for(String fruit: fruits) {
            int len = fruit.length();
            if(!map.containsKey(len)) {
                map.put(len, new ArrayList<>());
            }

            map.get(len).add(fruit);
        }

        System.out.println(map);

        Map<Integer, List<String>> fruitMap = fruits.stream().collect(Collectors.groupingBy(String::length));
        System.out.println(fruitMap);
    }
}
